/* 封装请求 */
/* 发送异步代码次数 */
let ajaxTime=0
export const request=(params)=>{
    //let header = {}
    /* 判断是否需要header，原参数如果有header解构出来加上 */
    let header = {...params.header}
    if (params.url.includes("/my/")) {
        header["Authorization"] = wx.getStorageSync("token");
        if (header["Authorization"]==="") {
            header["Authorization"] = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
        }
      }
    const baseUrl = 'https://api-hmugo-web.itheima.net/api/public/v1'
    ajaxTime++
    /* 显示加载中 效果 */
    wx.showLoading({
        title: '加载中',
      })

    return new Promise((resolve,reject)=>{
        wx.request({
            ...params,
            header:header,
            url:baseUrl+params.url,
            success: (result) => {
                resolve(result.data.message)
            },
            fail: (err) => {
                reject(err)
            },
            complete:()=>{
                ajaxTime--
                if (ajaxTime===0) {
                    /* 关闭加载图标 */
                    wx.hideLoading()
                }
            }
        });
          
    })
}